package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;

import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarsInfo3Binding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CarsSourcesEntiity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.home.VehicleOwnerAdapter;
import com.sgb.kjwl.view.widget.CarsInfoMorePopWindow3;
import com.sgb.kjwl.view.widget.GoodsMessagePopWindow3;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.viewmodel.transport.CarsInfoModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
/**
 *找车信息首页
 */
public class CarsInfoActivity3 extends AppActivity {

    private ActivityCarsInfo3Binding binding;

    private int LOCATION_TYPE = -1;//0发货地，1目的地
    private CarsInfoMorePopWindow3 carsInfoMorePopWindow;
    private VehicleOwnerAdapter vehicleOwnerAdapter;

    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private SendGoodsLocationPopWindow3 destinationPopWindow;
    private GoodsMessagePopWindow3 messagePopWindow;
    private List<GoodsInfoTypeEntity3> carsTypeData;
    private List<GoodsInfoTypeEntity3> carsLengthData;

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private int page = 1;
    private boolean isLoadMore = false;

    @Inject
    CarsInfoModel3 carsInfoModel;

    public static void start(Context context){
        Intent intent = new Intent();
        intent.setClass(context, CarsInfoActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent
                .builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cars_info3);
        binding.setViewModel(carsInfoModel);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("找车信息");
        binding.sendLocationLayout.setOnClickListener(this);
        binding.purposeLocationLayout.setOnClickListener(this);
        binding.carsMoreLayout.setOnClickListener(this);
        binding.pushSource.setOnClickListener(this);
        binding.joinBusiness.setOnClickListener(this);
        binding.joinPrice.setOnClickListener(this);
        binding.messageNumLayout.setOnClickListener(this);

        if (vehicleOwnerAdapter == null) {
            vehicleOwnerAdapter = new VehicleOwnerAdapter(getActivity(), null);
        }
        binding.carsList.setLayoutManager(new GridLayoutManager(this,1));
        binding.carsList.setAdapter(vehicleOwnerAdapter);
        vehicleOwnerAdapter.setOnItemClick(new AdapterOnItemClick<CarsSourcesEntiity>() {
            @Override
            public void onItemClick(CarsSourcesEntiity carsInfoEntity3, int position) {
                LogUtil.v("carsInfo=",carsInfoEntity3.getNo()+"");

                CarsInfoDetailActivity3.start(CarsInfoActivity3.this,carsInfoEntity3.getNo()+"");
            }
        });
        binding.carsList.setLoadingMoreEnabled(true);
        binding.carsList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                isLoadMore = false;
                carsInfoModel.getCarsInfoList(page);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page+1;
                carsInfoModel.getCarsInfoList(page);
            }
        });

        //发货省份popWindow
        sendGoodsLocationPopWindow3 = new SendGoodsLocationPopWindow3(getActivity());
        sendGoodsLocationPopWindow3.setBgView(binding.grayLayout);
        sendGoodsLocationPopWindow3.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvSend.setText(entity.getCityName());
                carsInfoModel.sendLocation.set(entity);

                page = 1;
                isLoadMore = false;
                carsInfoModel.getCarsInfoList(page);
                LogUtil.v("goodsInfo","发货物地址="+entity.getProvinceName()+"=="+entity.getCityName()+"=="+entity.getAreaName());
            }
        });
        sendGoodsLocationPopWindow3.setOnDismissListener(() -> binding.sendGoodsIv.setSelected(false));

        //收货省份popWindow
        destinationPopWindow = new SendGoodsLocationPopWindow3(getActivity());
        destinationPopWindow.setBgView(binding.grayLayout);
        destinationPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvDestination.setText(entity.getCityName());
                carsInfoModel.purposeLocation.set(entity);

                page = 1;
                isLoadMore = false;
                carsInfoModel.getCarsInfoList(page);
                LogUtil.v("goodsInfo","货物目的地址="+entity.getProvinceName()+"=="+entity.getCityName()+"=="+entity.getAreaName());
            }
        });
        destinationPopWindow.setOnDismissListener(() -> binding.purposeGoodsIv.setSelected(false));

        //更多
        carsInfoMorePopWindow = new CarsInfoMorePopWindow3(this);
        carsInfoMorePopWindow.setBgView(binding.bgPopGrayLayout);
        carsInfoMorePopWindow.setOnDismissListener(() -> binding.goodsMoreIv.setSelected(false));
        carsInfoMorePopWindow.setOnResultCallBack(new BasePopWindow.OnResultCallBack() {
            @Override
            public void result(Object result) {
                GoodsSourceFilterEntity filterEntity = (GoodsSourceFilterEntity)result;
                carsInfoModel.filterEntity.set(filterEntity);

                page = 1;
                isLoadMore = false;
                carsInfoModel.getCarsInfoList(page);
            }
        });

        //消息
        messagePopWindow = new GoodsMessagePopWindow3(this);
        messagePopWindow.setBgView(binding.bgPopGrayLayout);

    }

    @Override
    protected void init() {

        carsInfoModel.getAddressModel();
        carsInfoModel.getCarsLength();
        carsInfoModel.getCarsType();

        carsInfoModel.getCarsInfoList(page);
    }

    //列表
    public void getCarsInfoData(List<CarsSourcesEntiity> list){
        binding.carsList.refreshComplete();
        if (isLoadMore) {
            binding.carsList.loadMoreComplete();
            if (list.size() > 0) {
                vehicleOwnerAdapter.addDatas(list);
            } else {
                binding.carsList.setLoadingMoreEnabled(false);
            }
            binding.carsList.setVisibility(View.VISIBLE);
        } else {
            binding.carsList.refreshComplete();
            if (list.size() > 0) {
                binding.carsList.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                vehicleOwnerAdapter.setDatas(list);
                binding.carsList.setLoadingMoreEnabled(true);
            } else {
                vehicleOwnerAdapter.setDatas(null);
                binding.carsList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.send_location_layout:
                LOCATION_TYPE = 0;
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showSendPopWindow();
                }else{
                    carsInfoModel.getAddressModel();
                }
                setOptionStatus(1);
                break;
            case R.id.purpose_location_layout:
                LOCATION_TYPE = 1;
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showDestinationPopWindow();
                }else{
                    carsInfoModel.getAddressModel();
                }
                setOptionStatus(2);
                break;
            case R.id.cars_more_layout:
                setOptionStatus(4);
                if(null!=carsTypeData && carsTypeData.size()>0 && null!=carsLengthData && carsLengthData.size()>0){
                    showMoreFilterPopWindow();
                }else{
                    carsInfoModel.getGoodsTypeData();
                }
                LogUtil.v("findGoods","more");
                break;
            case R.id.push_source:
                break;
            case R.id.join_price:
                CarsInfoJoinQuotedActivity3.start(CarsInfoActivity3.this);
                break;
            case R.id.message_num_layout:
                //消息
                messagePopWindow.showPopwindowRight(binding.messageNumLayout);
                break;
            case R.id.join_business:

                break;
        }
    }

    //获取车型
    public void getCarsTypeData(List<GoodsInfoTypeEntity3> list){
        if(null!=list && list.size()>0){
            carsTypeData = list;
        }else{
            MToast.showToast(getActivity(),"数据未加载");
        }
    }

    //获取车长
    public void getCarsLengthData(List<GoodsInfoTypeEntity3> list){
        if(null!=list && list.size()>0){
            carsLengthData = list;
        }else{
            MToast.showToast(getActivity(),"数据未加载");
        }
    }

    private void showSendPopWindow(){
        sendGoodsLocationPopWindow3.showPopwindow(binding.sendLocationLayout,0,0);
        sendGoodsLocationPopWindow3.setData(addressEntityList);
    }

    private void showDestinationPopWindow(){
        destinationPopWindow.showPopwindow(binding.sendLocationLayout,0,0);
        destinationPopWindow.setData(addressEntityList);
    }

    private void showMoreFilterPopWindow(){
        carsInfoMorePopWindow.showPopwindowRight(binding.carsMoreLayout);
        carsInfoMorePopWindow.setData(carsTypeData,carsLengthData);
    }

    private void setOptionStatus(int index){
        switch (index){
            case 1:
                binding.tvSend.setSelected(true);
                binding.sendGoodsIv.setSelected(true);
                break;
            case 2:
                binding.tvDestination.setSelected(true);
                binding.purposeGoodsIv.setSelected(true);
                break;
            case 4:
                binding.tvMore.setSelected(true);
                binding.goodsMoreIv.setSelected(true);
                break;
        }
    }

    private int dp2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        Log.v("public==source",datas.size()+"");
        initJsonData();
    }

    private void initJsonData() {

        for (int i = 0; i < addressEntityList.size(); i++) {

//            cityList = new ArrayList<>();
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }

        if(LOCATION_TYPE == 1){
            showDestinationPopWindow();
        }else if(LOCATION_TYPE == 0){
            showSendPopWindow();
        }
    }
}
